home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / unix / volume10 / ifp / part01 next >
Encoding:
Internet Message Format  |  1987-07-05  |  25.8 KB

  1. Path: uunet!rs
  2. From: rs@uunet.UU.NET (Rich Salz)
  3. Newsgroups: comp.sources.unix
  4. Subject: v10i034: Interpreted Functional Programming lanuage, Part 01/07
  5. Message-ID: <571@uunet.UU.NET>
  6. Date: 7 Jul 87 04:32:07 GMT
  7. Organization: UUNET Communications Services, Arlington, VA
  8. Lines: 1224
  9. Approved: rs@uunet.uu.net
  10.  
  11. Mod.sources: Volume 10, Number 34
  12. Submitted by: robison@b.cs.uiuc.edu (Arch Robison)
  13. Archive-name: ifp/Part01
  14.  
  15. IFP[1] is a variant of Backus' FP [2].  The IFP interpreter is written
  16. in C and is an order of magnitude faster than the BSD 4.2 FP [3].
  17. The interpreter should run on any UNIX box.
  18.  
  19. Arch D. Robison
  20. University of Illinois at Urbana-Champaign
  21.     
  22. CSNET: robison@UIUC.CSNET
  23. UUCP: {ihnp4,pur-ee,convex}!uiucdcs!robison
  24. ARPA: robison@B.CS.UIUC.EDU (robison@UIUC.ARPA)
  25.  
  26. General References
  27. ------------------
  28.  
  29. [1]  Arch D. Robison, "Illinois  Functional  Programming:  A
  30.      Tutorial," BYTE Vol. 12,2 pp. 115-125 McGraw-Hill Inc.,
  31.      (February 1987).
  32.  
  33. [2]  John Backus, "Can Programming Be Liberated from the von
  34.      Neumann  Style?   A Functional Style and Its Algebra of
  35.      Programs," CACM  Vol.  21,8 pp.  613-641  ACM,  (August
  36.      1978).
  37.  
  38. [3]  Scott Baden, "Berkeley FP  User's  Manual,  Rev.  4.1,"
  39.      UNIX Programmers Manual, (July 27,1983).
  40.  
  41.  
  42. #! /bin/sh
  43. # This is a shell archive, meaning:
  44. # 1. Remove everything above the #! /bin/sh line.
  45. # 2. Save the resulting text in a file.
  46. # 3. Execute the file with /bin/sh.
  47. # The following files will be created:
  48. #    README
  49. #    ifp.1
  50. #    RunIFP
  51. #    fproot
  52. export PATH; PATH=/bin:$PATH
  53. if test -f 'README'
  54. then
  55.     echo shar: over-writing existing file "'README'"
  56. fi
  57. cat << \SHAR_EOF > 'README'
  58. This directory contains the following subdirectories and files:
  59.  
  60.     README - this file
  61.  
  62.     RunIFP - shell script which runs IFP interpreter
  63.  
  64.     fproot - IFP demonstration root directory
  65.     fproot/demo - demonstration functions
  66.  
  67.     interp - source code and Makefile for IFP interpreter
  68.  
  69.     manual - troff files and Makefile for IFP manual
  70. SHAR_EOF
  71. chmod +x 'README'
  72. if test -f 'ifp.1'
  73. then
  74.     echo shar: over-writing existing file "'ifp.1'"
  75. fi
  76. cat << \SHAR_EOF > 'ifp.1'
  77. .TH IFP 1 "Jan 28, 1987"
  78. .UC 4
  79. .SH NAME
  80. ifp \- Illinois FP interpreter 
  81. .SH SYNOPSIS
  82. .B ifp
  83. [ option ] 
  84. .SH DESCRIPTION
  85. .I Ifp
  86. starts the Illinois FP interpreter.
  87. .PP
  88. .I Ifp
  89. understands several options.  Normally, no options are required.
  90. Most of the options are for experimental work.  
  91. Not all (or even any) of the options may
  92. exist for a particular version of the interpreter.
  93. .TP
  94. .B \-l
  95. Print all functions with full pathname.  
  96. Useful for debugging scope problems.
  97. .TP
  98. .B \-e[012]
  99. Turn on expression cache.  The cache may be turned on for any subset of:
  100. .nf
  101.     0    Primitive functions
  102.     1    User-defined functions
  103.     2    PFOs
  104. .fi
  105. To see the cache statistics, give the command "cache" to the interpreter.
  106. .TP
  107. .B \-pn
  108. (Multimax only) This option specifies that the interpreter should be run
  109. as n parallel processes.
  110. .TP
  111. .B \-f
  112. (Multimax only) Print expression queue statistics.
  113. .TP 
  114. .B  \-sn
  115. (Multimax only) Use stack of max. depth n for expression scheduling.
  116. .TP
  117. .B \-qn
  118. (Multimax only) Use queue of max. length n for expression scheduling.
  119. .TP
  120. .B \-d[pafricxhusle]
  121. Turn on spy points corresponding to selected letters.
  122. .nf
  123.     p    parser
  124.     a    memory allocation
  125.     f    file io
  126.     r    reference counts
  127.     i    initialization
  128.     c    expression cache
  129.     x    extended definitions
  130.     h    hypercube
  131.     u    Multimax
  132.     s    Semaphores
  133.     l    Free list
  134.     e    Expression sckeduling
  135. .fi
  136. For example, -dar turns on spy points for memory allocation and 
  137. reference counts.
  138. .SH FILES
  139. .ta \w'/usr/local/lib/lib*.a\ \ 'u
  140. ifp    interpreter 
  141. .br
  142. .SH "SEE ALSO"
  143. Illinois FP User's Manual
  144. .sp 2
  145. A Functional Programming Interpreter (THESIS, Univerisity of Illinois) 
  146. .sp 2
  147. .ul
  148. Illinois Functional Programming: A Tutorial 
  149. (BYTE, Feb. 1987)
  150. .br
  151. fp(1)
  152. .SH BUGS
  153. Programming in IFP still requires thinking.  Some the options are incompatible.
  154. .SH AUTHOR
  155. Arch D. Robison
  156. SHAR_EOF
  157. if test -f 'RunIFP'
  158. then
  159.     echo shar: over-writing existing file "'RunIFP'"
  160. fi
  161. cat << \SHAR_EOF > 'RunIFP'
  162. # Shell script for invoking IFP interpreter.
  163.  
  164. # This script is for demonstration and documentation purposes only.
  165. # Running the script will set the appropriate environment variables
  166. # and start up the IFP interpreter in the demonstration directory.
  167.  
  168. # All its actions should be put in the user's .login, and .cshrc files.
  169. # The IFP interpreter must be compiled before running this script.
  170. # See the Makefile in subdirectory "interp" for instructions on how
  171. # to compile the interpreter.
  172.  
  173. #-----------------------------------------------------------------------
  174.  
  175. # These two lines should go in each user's .login file and be customized
  176. # for the user's particular IFP root directory and editor. 
  177.  
  178.     setenv IFProot $cwd/fproot
  179.     setenv EDITOR "/usr/ucb/vi"
  180.  
  181. # This alias is for convenience.  Presumably the interpreter will normally
  182. # reside on the user's search path.
  183.  
  184.     alias ifp $cwd/interp/ifp
  185.  
  186. # Normally the user would type in the following two lines:
  187.  
  188.     cd fproot/demo
  189.     ifp
  190.  
  191. # The interpreter will respond with the prompt "ifp>" when ready for 
  192. # user input.
  193.  
  194. SHAR_EOF
  195. chmod +x 'RunIFP'
  196. if test ! -d 'fproot'
  197. then
  198.     mkdir 'fproot'
  199. fi
  200. cd 'fproot'
  201. if test ! -d 'demo'
  202. then
  203.     mkdir 'demo'
  204. fi
  205. cd 'demo'
  206. if test -f '%IMPORT'
  207. then
  208.     echo shar: over-writing existing file "'%IMPORT'"
  209. fi
  210. cat << \SHAR_EOF > '%IMPORT'
  211. FROM /sys IMPORT
  212.    apndl,apndr,apply,assoc,cat,distl,distr,dropl,dropr,explode,id,implode,
  213.    iota,length,patom,pick,repeat,reverse,takel,taker,tl,tlr,trans;
  214.  
  215. FROM /math/arith IMPORT
  216.    +,-,*,%,add1,arccos,arcsin,arctan,cos,div,exp,ln,
  217.    max,min,mod,minus,power,prod,sin,sqrt,sub1,sum,tan;
  218.  
  219. FROM /math/logic IMPORT
  220.    <,<=,=,~=,>=,>,~,and,all,any,atom,boolean,false,longer,member,null,
  221.    numeric,odd,or,pair,shorter,xor; 
  222.  
  223. SHAR_EOF
  224. if test -f 'A'
  225. then
  226.     echo shar: over-writing existing file "'A'"
  227. fi
  228. cat << \SHAR_EOF > 'A'
  229. DEF A AS
  230.   #<<1 2 3>
  231.     <4 5 6>
  232.     <7 8 9>>;
  233. SHAR_EOF
  234. if test -f 'Abs'
  235. then
  236.     echo shar: over-writing existing file "'Abs'"
  237. fi
  238. cat << \SHAR_EOF > 'Abs'
  239. (* Absolute value function *)
  240.  
  241. DEF Abs AS
  242.    IF [id,#0] | < THEN minus
  243.    ELSE id
  244.    END;
  245. SHAR_EOF
  246. if test -f 'B'
  247. then
  248.     echo shar: over-writing existing file "'B'"
  249. fi
  250. cat << \SHAR_EOF > 'B'
  251. (*
  252.  * A data matrix.
  253.  *)
  254. DEF B AS
  255.    #<5 3 88  6 21  0  0 -7 
  256.      5 2  1  4  8 10  1  4
  257.      2 4  3  7  9  5  2  5
  258.      1 3  3  5  7  2  3  6
  259.      4 7  5  3  1  8  4  7
  260.      8 9  7  1  6 12  5  8
  261.     10 5  2  8 12  1  6  9
  262.      1 2  3  4  5  6  1  2
  263.      4 5  6  7  8  9  2  1>;
  264.  
  265. SHAR_EOF
  266. if test -f 'Cart'
  267. then
  268.     echo shar: over-writing existing file "'Cart'"
  269. fi
  270. cat << \SHAR_EOF > 'Cart'
  271. (* 
  272.  * Cartesian product of two sequences
  273.  *)
  274. DEF Cart AS 
  275.    distr | EACH distl END;
  276.  
  277. SHAR_EOF
  278. if test -f 'CosSin'
  279. then
  280.     echo shar: over-writing existing file "'CosSin'"
  281. fi
  282. cat << \SHAR_EOF > 'CosSin'
  283. (*
  284.  * CosSin
  285.  *
  286.  * Compute the sine and cosine of an angle via half-angle formulae.
  287.  * The function is accurate to 12 decimals.
  288.  *)
  289. DEF CosSin AS
  290.    IF [id|Abs,#1e-6]|< THEN [[#1,[Square,#2]|%]|-,id]
  291.    ELSE
  292.       [id,#2]|% | CosSin | 
  293.       [[+,-]|*, [*,*]|+] 
  294.    END;
  295.  
  296. SHAR_EOF
  297. if test -f 'Data'
  298. then
  299.     echo shar: over-writing existing file "'Data'"
  300. fi
  301. cat << \SHAR_EOF > 'Data'
  302. (*
  303.  * Currently I/O is almost non-existent in IFP.  One way to skirt this problem
  304.  * when you have a large amount of data to input is to define a constant 
  305.  * function with the appropriate values.
  306.  *
  307.  * The function Data returns a sequence of data values.  We could, for 
  308.  * instance, sort them by composing Data with QuickSort
  309.  *
  310.  * Examples:
  311.  *      0 : Data -> <5 3 88 6 21 0 -7>
  312.  *      0 : Data | QuickSort -> <-7 0 3 5 6 21 88>
  313.  *)
  314.  
  315. DEF Data AS
  316.    #<5 3 88 6 21 0 -7>;
  317.  
  318. SHAR_EOF
  319. if test -f 'Debug'
  320. then
  321.     echo shar: over-writing existing file "'Debug'"
  322. fi
  323. cat << \SHAR_EOF > 'Debug'
  324. (*
  325.  * Version of Fib with debugging output
  326.  *
  327.  * n : Debug -> sequence of first n Fibonacci numbers
  328.  *
  329.  * The two functions created with the "@" form print their inputs
  330.  * so we can see what is going on inside the Debug function.
  331.  *)
  332.  
  333. DEF Debug AS
  334.  
  335.    IF [id,#2] | <= THEN
  336.  
  337.       @"trivial case" | [#<1 1>,id] | takel
  338.  
  339.    ELSE
  340.  
  341.       @"recurse" | sub1 | Debug | [id, [1r,2r]|+] | apndr
  342.  
  343.    END;
  344.  
  345. SHAR_EOF
  346. if test -f 'Double'
  347. then
  348.     echo shar: over-writing existing file "'Double'"
  349. fi
  350. cat << \SHAR_EOF > 'Double'
  351. (* Double a number - e.g. 3:Double -> 6 *)
  352.  
  353. DEF Double AS [id,id]|+;
  354.  
  355. SHAR_EOF
  356. if test -f 'Fib'
  357. then
  358.     echo shar: over-writing existing file "'Fib'"
  359. fi
  360. cat << \SHAR_EOF > 'Fib'
  361. (*
  362.  * Compute nth fibonacci number
  363.  *
  364.  * Examples:
  365.  *
  366.  *    6 : Fib -> 8
  367.  *    7 : Fib -> 13
  368.  *    8 : Fib -> 21
  369.  *)
  370.  
  371. DEF Fib AS 
  372.    IF [id,#2] | < THEN id
  373.    ELSE
  374.       sub1 | [Fib,sub1|Fib] | +
  375.    END;
  376. SHAR_EOF
  377. if test -f 'FibSeq'
  378. then
  379.     echo shar: over-writing existing file "'FibSeq'"
  380. fi
  381. cat << \SHAR_EOF > 'FibSeq'
  382. (*
  383.  * Fibonacci numbers
  384.  * 
  385.  * n : FibSeq -> sequence of first n Fibonacci numbers
  386.  *
  387.  * Example
  388.  *      6 : FibSeq -> <1 1 2 3 5 8>
  389.  *)
  390.  
  391. DEF FibSeq AS
  392.    IF [id,#2] | <= THEN
  393.       [#<1 1>,id] | takel       (* trivial case *)
  394.    ELSE
  395.       sub1 | FibSeq |           (* generate n-1 Fibonacci numbers        *)
  396.       [id, [1r,2r]|+] | apndr   (* append sum of last two to end of list *)
  397.    END;
  398.  
  399. SHAR_EOF
  400. if test -f 'Format'
  401. then
  402.     echo shar: over-writing existing file "'Format'"
  403. fi
  404. cat << \SHAR_EOF > 'Format'
  405. DEF Format AS
  406.    [id,#" "] | distr | cat | implode;
  407.  
  408. SHAR_EOF
  409. if test -f 'Hanoi'
  410. then
  411.     echo shar: over-writing existing file "'Hanoi'"
  412. fi
  413. cat << \SHAR_EOF > 'Hanoi'
  414. (*
  415.  * Hanio - towers of Hanoi solver
  416.  *
  417.  * <N Names> : Hanoi -> <<src dst> ...<src dst>>
  418.  *
  419.  * where N is number of rings and names is a triple of post names
  420.  * <source temporary dest>.
  421.  *
  422.  * Example:
  423.  *    <2 <a b c>> : Hanoi -> <<a,b>,<a,c>,<b,c>>
  424.  *)
  425.  
  426. DEF Hanoi AS
  427.    {[n,[a,b,c]] := id}
  428.    IF [n,#0]|= THEN []
  429.    ELSE [
  430.       [n|sub1, [a,c,b]] | Hanoi,
  431.       [[a,c]],
  432.       [n|sub1, [b,a,c]] | Hanoi 
  433.    ] | cat
  434.    END;
  435.  
  436. SHAR_EOF
  437. if test -f 'Inner'
  438. then
  439.     echo shar: over-writing existing file "'Inner'"
  440. fi
  441. cat << \SHAR_EOF > 'Inner'
  442. (*
  443.  * Compute the inner product of two vectors.
  444.  *)
  445. DEF Inner AS 
  446.    trans | EACH * END | INSERT + END;
  447. SHAR_EOF
  448. if test -f 'InsertSort'
  449. then
  450.     echo shar: over-writing existing file "'InsertSort'"
  451. fi
  452. cat << \SHAR_EOF > 'InsertSort'
  453. (*
  454.  * InsertSort
  455.  *
  456.  * This function sorts a sequence of numbers or strings into ascending order
  457.  * using insertion sort.
  458.  *
  459.  * Examples:
  460.  *
  461.  *      <3 1 4 1 5 9 2> : InsertSort == <1 1 2 3 4 5 9>
  462.  *
  463.  *      <all work and no play> : InsertSort == <all and no play work>
  464.  *
  465.  * The sequence may not mix strings and numbers.
  466.  *)
  467. DEF InsertSort AS
  468.    IF null THEN id            (* Check for trivial case *)
  469.    ELSE
  470.       [tl,[1]] | apndr | 
  471.       INSERT
  472.      {[Element,Seq] := id}
  473.          {[Left,Right] := [Seq, distl | FILTER > END | length] | [takel,dropl]}
  474.      [Left,[Element],Right] | cat
  475.       END
  476.    END;
  477.  
  478. SHAR_EOF
  479. if test -f 'Inter'
  480. then
  481.     echo shar: over-writing existing file "'Inter'"
  482. fi
  483. cat << \SHAR_EOF > 'Inter'
  484. (*
  485.  * Set intersection: [A,B] : Inter -> elements of B which are in A 
  486.  * 
  487.  * Example:
  488.  *      <<2 3 4> <5 3 2 1>> : Inter -> <3 2>
  489.  *)
  490.  
  491. DEF Inter AS
  492.    distl | FILTER member END | EACH 2 END;
  493. SHAR_EOF
  494. if test -f 'MatMul'
  495. then
  496.     echo shar: over-writing existing file "'MatMul'"
  497. fi
  498. cat << \SHAR_EOF > 'MatMul'
  499. (*
  500.  * MatMul
  501.  *
  502.  * [matrixA,matrixB] -> matrixAB
  503.  *)
  504. DEF MatMul AS
  505.    {[A,B] := id}
  506.    [A,B|trans] | distr |
  507.    EACH distl |
  508.       EACH Inner END
  509.    END;  
  510. SHAR_EOF
  511. if test -f 'Member'
  512. then
  513.     echo shar: over-writing existing file "'Member'"
  514. fi
  515. cat << \SHAR_EOF > 'Member'
  516. (*
  517.  * <S e> : Member -> #t if e is in sequence S, #f otherwise
  518.  *
  519.  * Examples:
  520.  *      <<a b c d> c> -> t
  521.  *      <<2 4 6 8> 3> -> f
  522.  *)
  523. DEF Member AS
  524.    distr | EACH = END | any;
  525.  
  526. SHAR_EOF
  527. if test -f 'Merge'
  528. then
  529.     echo shar: over-writing existing file "'Merge'"
  530. fi
  531. cat << \SHAR_EOF > 'Merge'
  532. (*
  533.  * Merge
  534.  *
  535.  * Merge two ascending sequences.
  536.  *
  537.  * The sequences should not mix strings and numbers.
  538.  *
  539.  * Example:
  540.  *
  541.  *    <<a b x z> <c d z>> : Merge -> <a b c d x y z>
  542.  *)
  543. DEF Merge AS
  544.    IF EACH null END | or THEN cat
  545.    ELSE
  546.       IF EACH 1 END | > THEN reverse ELSE id END |
  547.       [1|1,[1|tl,2]|Merge] | apndl
  548.    END;
  549.  
  550. SHAR_EOF
  551. if test -f 'MergeSort'
  552. then
  553.     echo shar: over-writing existing file "'MergeSort'"
  554. fi
  555. cat << \SHAR_EOF > 'MergeSort'
  556. (*
  557.  * MergeSort
  558.  *
  559.  * This function sorts a sequence of numbers or strings into ascending order
  560.  * using merge sort.
  561.  *
  562.  * Examples:
  563.  *
  564.  *      <3 1 4 1 5 9 2> : MergeSort == <1 1 2 3 4 5 9>
  565.  *
  566.  *      <all work and no play> : MergeSort == <all and no play work>
  567.  *
  568.  * The sequence may not mix strings and numbers.
  569.  *)
  570. DEF MergeSort AS
  571.    IF [length,#2] | < THEN id
  572.    ELSE
  573.        [id, [length,#2] | div] |
  574.        [takel,dropl] | EACH MergeSort END | Merge
  575.    END;
  576.  
  577. SHAR_EOF
  578. if test -f 'Outer'
  579. then
  580.     echo shar: over-writing existing file "'Outer'"
  581. fi
  582. cat << \SHAR_EOF > 'Outer'
  583. (* 
  584.  * Outer product of two vectors 
  585.  *)
  586. DEF Outer AS 
  587.    Cart | EACH EACH * END END;
  588.  
  589. SHAR_EOF
  590. if test -f 'PasTri'
  591. then
  592.     echo shar: over-writing existing file "'PasTri'"
  593. fi
  594. cat << \SHAR_EOF > 'PasTri'
  595. (*
  596.  * Pascal's triangle generator
  597.  *
  598.  * n : PasTri -> first n rows of Pascal's triangle
  599.  *
  600.  * Example
  601.  *              5 : PasTri -> <
  602.  *                               <1>
  603.  *                               <1 1>
  604.  *                               <1 2 1>
  605.  *                               <1 3 3 1>
  606.  *                               <1 4 6 4 1>
  607.  *                            >
  608.  *)
  609.  
  610. DEF PasTri AS          
  611.    IF [id,#0] | <= THEN #<<1>>
  612.    ELSE
  613.       sub1 | PasTri |                (* Create triangle with n-1 rows *)
  614.       [
  615.          id,  
  616.  
  617.          1r |                        (* Take last row of smaller triangle *)
  618.          [
  619.             [#0,id] | apndl,         (* Append 0 to left edge of row  *)
  620.             [id,#0] | apndr          (* Append 0 to right edge of row *)
  621.          ] | trans | EACH + END      (* Add corresponding elements *)
  622.  
  623.       ] | apndr                      (* Append new row to smaller triangle *)
  624.    END;
  625.  
  626.  
  627.  
  628.  
  629. SHAR_EOF
  630. if test -f 'Permute'
  631. then
  632.     echo shar: over-writing existing file "'Permute'"
  633. fi
  634. cat << \SHAR_EOF > 'Permute'
  635. DEF Permute AS
  636.    IF null THEN #<<>>
  637.    ELSE
  638.       [id,length|iota] | distl |
  639.       EACH
  640.      [pick,[takel|tlr,dropl]|cat|Permute] | distl | EACH apndl END
  641.       END | cat
  642.    END; 
  643. SHAR_EOF
  644. if test -f 'PigLatin'
  645. then
  646.     echo shar: over-writing existing file "'PigLatin'"
  647. fi
  648. cat << \SHAR_EOF > 'PigLatin'
  649. (*
  650.  * Convert sequence of words to pig-latin
  651.  *
  652.  * E.g.
  653.  *      <Have a nice day> : PigLatin -> <aveHay a icenay ayday>
  654.  *
  655.  * The text processing is done via explode and implode.
  656.  * `explode' converts a string into a sequence of characters (1 letter strings),
  657.  * `implode' catenates a sequence of strings into a single string.
  658.  *
  659.  * Coded by Kevin Kenny and Arch Robison
  660.  *)
  661.  
  662. DEF PigLatin AS
  663.  
  664.    EACH
  665.       explode |
  666.  
  667.       IF EACH Vowel END | any | ~ THEN id    (* Not a word, don't mangle it *)
  668.       ELSE
  669.  
  670.          IF 1|Vowel|~ THEN
  671.  
  672.             (* Word begins with a consonant - rotate first vowel to front. *)
  673.  
  674.             WHILE 1|Vowel|~ DO
  675.                [tl,1]|apndr
  676.             END
  677.  
  678.          ELSIF [1r|Vowel, [#<y Y>,1r]|member] | or THEN
  679.  
  680.             (* Word ends in vowel or 'y' - append a 'w' *)
  681.  
  682.             [id,#w] | apndr
  683.  
  684.          ELSE id
  685.          END |
  686.  
  687.          [id,#<a y>] | cat
  688.  
  689.       END |
  690.  
  691.       implode
  692.    END;
  693.  
  694. SHAR_EOF
  695. if test -f 'PowerSet'
  696. then
  697.     echo shar: over-writing existing file "'PowerSet'"
  698. fi
  699. cat << \SHAR_EOF > 'PowerSet'
  700. (*
  701.  * PowerSet
  702.  *
  703.  * This function generates all subsets of a given set.  Sets are 
  704.  * represented as sequences of distinct elements.
  705.  *
  706.  * Examples:
  707.  *
  708.  *      <> : PowerSet -> <<>>
  709.  *
  710.  *      <a b c> : PowerSet -> <<a,b,c>,<a,b>,<a,c>,<a>,<b,c>,<b>,<c>,<>>
  711.  *)
  712.  
  713. DEF PowerSet AS
  714.    IF null THEN [id] 
  715.    ELSE 
  716.       [1, tl | PowerSet] |
  717.       [
  718.          distl | EACH apndl END,
  719.      2 
  720.       ] 
  721.       | cat 
  722.    END;
  723.  
  724. SHAR_EOF
  725. if test -f 'QuickSort'
  726. then
  727.     echo shar: over-writing existing file "'QuickSort'"
  728. fi
  729. cat << \SHAR_EOF > 'QuickSort'
  730. (*
  731.  * QuickSort
  732.  *
  733.  * This function sorts a sequence of numbers or strings into ascending order
  734.  * using the Quicksort algorithm.
  735.  *
  736.  * Examples:
  737.  *
  738.  *      <3 1 4 1 5 9 2> : QuickSort == <1 1 2 3 4 5 9>
  739.  *
  740.  *      <all work and no play> : QuickSort == <all and no play work>
  741.  *
  742.  * The sequence may not mix strings and numbers.
  743.  *)
  744.  
  745. DEF QuickSort AS
  746.    IF [length,#2] | < THEN id
  747.    ELSE
  748.       [id,1] | distr |
  749.       [      
  750.          FILTER < END | EACH 1 END | QuickSort,
  751.          FILTER = END | EACH 1 END,
  752.          FILTER > END | EACH 1 END | QuickSort
  753.       ] | cat
  754.    END;
  755.  
  756. SHAR_EOF
  757. if test -f 'SelSort'
  758. then
  759.     echo shar: over-writing existing file "'SelSort'"
  760. fi
  761. cat << \SHAR_EOF > 'SelSort'
  762. (*
  763.  * Selection sort
  764.  *
  765.  * Example:
  766.  *
  767.  *      <3 1 4 1 5 9 2> : SelSort == <1 1 2 3 4 5 9>
  768.  *
  769.  * The sequence must be numeric.
  770.  *)
  771. DEF SelSort AS
  772.    IF [length,#2] | < THEN id
  773.    ELSE 
  774.       [INSERT min END,id] | distl |
  775.       [
  776.          FILTER  = END | EACH 2 END,
  777.          FILTER ~= END | EACH 2 END | SelSort
  778.       ] | cat
  779.    END;
  780.  
  781. SHAR_EOF
  782. if test -f 'SlowSort'
  783. then
  784.     echo shar: over-writing existing file "'SlowSort'"
  785. fi
  786. cat << \SHAR_EOF > 'SlowSort'
  787. (*
  788.  * SlowSort
  789.  *
  790.  * Sort a sequence the hard way, i.e. take all permutations
  791.  * and pick one for which all elements are in order.
  792.  *)
  793. DEF SlowSort AS
  794.    Permute | 
  795.    FILTER 
  796.       [tl,tlr]|trans | EACH >= END | all 
  797.    END | 1;
  798.  
  799. SHAR_EOF
  800. if test -f 'Square'
  801. then
  802.     echo shar: over-writing existing file "'Square'"
  803. fi
  804. cat << \SHAR_EOF > 'Square'
  805. (* Square a number - e.g. 3:Square -> 9 *)
  806.  
  807. DEF Square AS [id,id]|*;
  808.  
  809. SHAR_EOF
  810. if test -f 'Tangent'
  811. then
  812.     echo shar: over-writing existing file "'Tangent'"
  813. fi
  814. cat << \SHAR_EOF > 'Tangent'
  815. (* Trigonometric tangent function *)
  816.  
  817. DEF Tangent AS [sin,cos]|%;
  818. SHAR_EOF
  819. if test -f 'Text'
  820. then
  821.     echo shar: over-writing existing file "'Text'"
  822. fi
  823. cat << \SHAR_EOF > 'Text'
  824. (*
  825.  * Text
  826.  * 
  827.  * Example piece of text for use with PigLatin function.
  828.  *
  829.  * Example:
  830.  *
  831.  *    0 : Text | PigLatin -> ...
  832.  *)
  833.  
  834. DEF Text AS
  835.    #<All work and no play makes Jack a dull boy>;
  836. SHAR_EOF
  837. if test -f 'Vowel'
  838. then
  839.     echo shar: over-writing existing file "'Vowel'"
  840. fi
  841. cat << \SHAR_EOF > 'Vowel'
  842. (*
  843.  * Determine if a letter is a vowel.
  844.  *
  845.  * E.g.
  846.  *      A : Vowel -> t
  847.  *      u : Vowel -> t
  848.  *      R : Vowel -> f
  849.  *
  850.  * This function is used by function PigLatin.
  851.  *)
  852.  
  853. DEF Vowel AS 
  854.    [#<a e i o u A E I O U>, id] | member;
  855. SHAR_EOF
  856. if test -f 'cotan'
  857. then
  858.     echo shar: over-writing existing file "'cotan'"
  859. fi
  860. cat << \SHAR_EOF > 'cotan'
  861. (*
  862.  * Trigonmetric cotangent function
  863.  *)
  864.  
  865. DEF cotan AS [cos,sin] | %;
  866.  
  867. SHAR_EOF
  868. if test -f 'Euler'
  869. then
  870.     echo shar: over-writing existing file "'Euler'"
  871. fi
  872. cat << \SHAR_EOF > 'Euler'
  873. (*
  874.  * Compute Euler's phi-function, i.e. number of number rel. prime to n.
  875.  *
  876.  * E.g. 8:Euler -> 4 since 1,3,5,7 are relatively prime to 8
  877.  *)
  878. DEF Euler AS
  879.    [id,iota] | distl |
  880.    EACH 
  881.       WHILE [2,#0]|> DO [2,mod] END |
  882.       IF [1, #1]|= THEN #1 ELSE #0 END 
  883.    END | sum;
  884.  
  885. SHAR_EOF
  886. if test -f '%DOC'
  887. then
  888.     echo shar: over-writing existing file "'%DOC'"
  889. fi
  890. cat << \SHAR_EOF > '%DOC'
  891. This directory contains demonstration functions.  To use one, enter the
  892. IFP command
  893.  
  894.      show input : function ;
  895.  
  896. where input is an appropriate object and function is one of the functions below:
  897.  
  898.      Abs - absolute value of a number
  899.      cotan - find trigonometric cotangent of angle expressed in radians
  900.      Debug - version of Fib with debugging messages
  901.      Double - double a number
  902.      Euler - Euler totient (phi) function
  903.      Fib - find first n fibonacci numbers
  904.      Inner - find inner product of two vectors
  905.      Inter - set intersection
  906.      Member - set membership
  907.      PigLatin - converts sequence of words to Pig Latin
  908.      PasTri - find first n rows of Pascal's triangle
  909.      QuickSort - sort a sequence of numbers or sequence of strings
  910.      Square - square a number
  911.      Vowel - determine if a string is a vowel     
  912.  
  913. All IFP primitives are imported into this directory also, so you can
  914. experiment with them.  Look in %IMPORT for their names.  The functions
  915. are detailed in the file \IFP.TXT
  916.  
  917. SHAR_EOF
  918. cd ..
  919. if test ! -d 'math'
  920. then
  921.     mkdir 'math'
  922. fi
  923. cd 'math'
  924. if test ! -d 'arith'
  925. then
  926.     mkdir 'arith'
  927. fi
  928. cd 'arith'
  929. if test -f 'prod'
  930. then
  931.     echo shar: over-writing existing file "'prod'"
  932. fi
  933. cat << \SHAR_EOF > 'prod'
  934. (*
  935.  * prod
  936.  *
  937.  * Compute product of sequence.
  938.  *
  939.  * E.g. <10 2 3 4> : prod -> 240
  940.  *)
  941.  
  942. DEF prod AS
  943.    IF ../logic/null THEN #1
  944.    ELSE
  945.       INSERT * END
  946.    END;
  947. SHAR_EOF
  948. cd ..
  949. if test ! -d 'linear'
  950. then
  951.     mkdir 'linear'
  952. fi
  953. cd 'linear'
  954. if test -f '%IMPORT'
  955. then
  956.     echo shar: over-writing existing file "'%IMPORT'"
  957. fi
  958. cat << \SHAR_EOF > '%IMPORT'
  959. FROM /sys IMPORT
  960.    apndl,apndr,cat,distr,distl,id,iota,length,tl,trans;
  961.  
  962. FROM /math/arith IMPORT
  963.    +,-,*,%,exp,ln,sum;
  964.  
  965. FROM /math/logic IMPORT =,null;
  966. SHAR_EOF
  967. if test -f 'A'
  968. then
  969.     echo shar: over-writing existing file "'A'"
  970. fi
  971. cat << \SHAR_EOF > 'A'
  972. (* Test case generator for L and U - check by trying A | [L,U] | MatMul *)
  973.  
  974. DEF A AS
  975.    #<
  976.        < 2.3  4.7 -2.7  5.7  7.4 2.1 12.7  1.1 32.1  4.5  1.1  8.3>
  977.        < 1.7 -1.7  5.2  3.2  1.2 3.5  2.4  2.9  1.9  1.7 -4.5 -9.9>
  978.        < 6.1  3.4  1.2 10.6  2.9 1.7  1.1 -0.3  1.2  3.2  1.6  1.3>
  979.        <23.3 -9.7  2.4  5.2  7.6 1.1 86.2  1.7  3.2  9.7  1.2 87.1>
  980.        < 1.2  3.4  4.5  6.7  9.8 0.1  2.1  5.7 -9.1 -5.2  0.2  1.7>
  981.        <12.3  1.2  8.7 12.3 -4.7 -.1  3.2  2.1  4.3  1.8  1.9  2.3>
  982.        < 5.7  4.7 -2.8  5.7  7.4 2.1 12.7  1.1 32.1  4.5  1.1  8.3>
  983.        < 1.7 -6.7  5.6  7.4  1.2 3.5  2.7  2.8  1.9  1.7 -4.5 -9.9>
  984.        < 3.1 -3.4 -9.2 10.6  8.9 1.7 -1.1 -0.3  3.2  3.2  1.6  1.3>
  985.        <13.3 -9.7  5.2  7.6 1.1 86.2  1.3  3.2  9.7  1.2 87.1 -9.2>
  986.        < 1.2  3.4 -4.5 -6.7  9.8 0.1 -2.1  5.8 -9.1 -5.2  0.2  1.7>
  987.        <12.3  1.2 -8.7 12.3 -4.7 -.1 -3.2  1.8  1.9  2.3  3.1  4.3> 
  988.    >;
  989.  
  990. SHAR_EOF
  991. if test -f 'Aik'
  992. then
  993.     echo shar: over-writing existing file "'Aik'"
  994. fi
  995. cat << \SHAR_EOF > 'Aik'
  996. (* Tail of matrix after gaussian elimination on (1|1) *)
  997.  
  998. DEF Aik AS 
  999.    [
  1000.       MatTail, 
  1001.       [Li1|tl,U1k|tl] | Outer
  1002.    ] | MatSub;
  1003.  
  1004. SHAR_EOF
  1005. if test -f 'ApndlCol'
  1006. then
  1007.     echo shar: over-writing existing file "'ApndlCol'"
  1008. fi
  1009. cat << \SHAR_EOF > 'ApndlCol'
  1010. (* Append column (1) to left side of matrix (2) *)
  1011.  
  1012. DEF ApndlCol AS [1,2|trans] | apndl | trans;
  1013.  
  1014. SHAR_EOF
  1015. if test -f 'ApndrCol'
  1016. then
  1017.     echo shar: over-writing existing file "'ApndrCol'"
  1018. fi
  1019. cat << \SHAR_EOF > 'ApndrCol'
  1020. (* Append column (2) to right side of matrix (1) *)
  1021. DEF ApndrCol [1|trans,2] | apndr | trans;
  1022.  
  1023. SHAR_EOF
  1024. if test -f 'MatTail'
  1025. then
  1026.     echo shar: over-writing existing file "'MatTail'"
  1027. fi
  1028. cat << \SHAR_EOF > 'MatTail'
  1029. (* Deletes first row and column of matrix *)
  1030. DEF MatTail AS tl | EACH tl END;
  1031.  
  1032. SHAR_EOF
  1033. if test -f 'Outer'
  1034. then
  1035.     echo shar: over-writing existing file "'Outer'"
  1036. fi
  1037. cat << \SHAR_EOF > 'Outer'
  1038. (* Outer product of two vectors *)
  1039. DEF Outer AS Cart | EACH EACH * END END;
  1040.  
  1041. SHAR_EOF
  1042. if test -f '%DOC'
  1043. then
  1044.     echo shar: over-writing existing file "'%DOC'"
  1045. fi
  1046. cat << \SHAR_EOF > '%DOC'
  1047. This directory contains functions for linear matrix manipulation.
  1048. In particular, it contains L and U, which take the lower and upper 
  1049. parts of the LU decomposition of a matrix.
  1050. SHAR_EOF
  1051. if test -f 'Singleton'
  1052. then
  1053.     echo shar: over-writing existing file "'Singleton'"
  1054. fi
  1055. cat << \SHAR_EOF > 'Singleton'
  1056. (* Check if matrix is a singleton *)
  1057.  
  1058. DEF Singleton AS [length,#1]|=;
  1059.  
  1060. SHAR_EOF
  1061. if test -f 'U'
  1062. then
  1063.     echo shar: over-writing existing file "'U'"
  1064. fi
  1065. cat << \SHAR_EOF > 'U'
  1066. (* U part of LU decomposition of matrix *)
  1067.  
  1068. DEF U AS
  1069.    IF Singleton THEN id
  1070.    ELSE
  1071.       [
  1072.          U1k,
  1073.      Aik | [EACH #0 END,U] | ApndlCol
  1074.       ] | apndl
  1075.    END;
  1076.  
  1077. SHAR_EOF
  1078. if test -f 'U1k'
  1079. then
  1080.     echo shar: over-writing existing file "'U1k'"
  1081. fi
  1082. cat << \SHAR_EOF > 'U1k'
  1083. (* First row of U part *)
  1084. DEF U1k AS 1;
  1085.  
  1086. SHAR_EOF
  1087. if test -f 'Cart'
  1088. then
  1089.     echo shar: over-writing existing file "'Cart'"
  1090. fi
  1091. cat << \SHAR_EOF > 'Cart'
  1092. (* Cartesian product of two vectors *)
  1093. DEF Cart AS distr | EACH distl END;
  1094.  
  1095. SHAR_EOF
  1096. if test -f 'Inner'
  1097. then
  1098.     echo shar: over-writing existing file "'Inner'"
  1099. fi
  1100. cat << \SHAR_EOF > 'Inner'
  1101. (* Inner product *)
  1102.  
  1103. DEF Inner AS 
  1104.    trans | EACH * END | 
  1105.    IF null THEN #0 
  1106.    ELSE INSERT + END 
  1107.    END;
  1108. SHAR_EOF
  1109. if test -f 'L'
  1110. then
  1111.     echo shar: over-writing existing file "'L'"
  1112. fi
  1113. cat << \SHAR_EOF > 'L'
  1114. (* L part of LU decomposition of matrix *)
  1115. DEF L AS
  1116.    IF Singleton THEN #<<1.0>>
  1117.    ELSE 
  1118.       [
  1119.          Li1,
  1120.      Aik | [EACH #0 END,L] | apndl
  1121.       ] | ApndlCol
  1122.    END;
  1123.  
  1124. SHAR_EOF
  1125. if test -f 'LU'
  1126. then
  1127.     echo shar: over-writing existing file "'LU'"
  1128. fi
  1129. cat << \SHAR_EOF > 'LU'
  1130. (* 
  1131.  * LU
  1132.  *
  1133.  * [L,U] decomposition of matrix written as single function.
  1134.  * This function is functionally identical to [L,U]
  1135.  *)
  1136.  
  1137. DEF LU AS
  1138.    IF Singleton THEN [#<<1.0>>,id]        (* definition of L *)
  1139.    ELSE 
  1140.       [
  1141.          Li1,
  1142.          Aik | [EACH #0 END,LU],
  1143.      U1k
  1144.       ] | 
  1145.       [
  1146.      [
  1147.         1,
  1148.         2 | [1,2|1] | apndl
  1149.      ] | ApndlCol,
  1150.      [
  1151.         3,
  1152.         2 | [1,2|2] | ApndlCol
  1153.      ] | apndl
  1154.       ]
  1155.    END;
  1156. SHAR_EOF
  1157. if test -f 'Li1'
  1158. then
  1159.     echo shar: over-writing existing file "'Li1'"
  1160. fi
  1161. cat << \SHAR_EOF > 'Li1'
  1162. (* First column of L part *)
  1163. DEF Li1 AS [EACH 1 END,1|1] | distr | EACH % END;
  1164.  
  1165. SHAR_EOF
  1166. if test -f 'MatAdd'
  1167. then
  1168.     echo shar: over-writing existing file "'MatAdd'"
  1169. fi
  1170. cat << \SHAR_EOF > 'MatAdd'
  1171. (* Matrix addition *)
  1172. DEF MatAdd AS MatCat | EACH EACH + END END;
  1173.  
  1174. SHAR_EOF
  1175. if test -f 'MatCat'
  1176. then
  1177.     echo shar: over-writing existing file "'MatCat'"
  1178. fi
  1179. cat << \SHAR_EOF > 'MatCat'
  1180. (* Converts pair of matrices to matrix of pairs *)
  1181.  
  1182. DEF MatCat AS trans | EACH trans END;
  1183.  
  1184. SHAR_EOF
  1185. if test -f 'MatMul'
  1186. then
  1187.     echo shar: over-writing existing file "'MatMul'"
  1188. fi
  1189. cat << \SHAR_EOF > 'MatMul'
  1190. DEF MatMul AS
  1191.    [1,2|trans] |
  1192.    distr |
  1193.    EACH distl |
  1194.       EACH Inner END
  1195.    END;  
  1196. SHAR_EOF
  1197. if test -f 'MatSub'
  1198. then
  1199.     echo shar: over-writing existing file "'MatSub'"
  1200. fi
  1201. cat << \SHAR_EOF > 'MatSub'
  1202. (* Matrix subtraction *)
  1203. DEF MatSub AS MatCat | EACH EACH - END END;
  1204.  
  1205. SHAR_EOF
  1206. if test -f 'VecAdd'
  1207. then
  1208.     echo shar: over-writing existing file "'VecAdd'"
  1209. fi
  1210. cat << \SHAR_EOF > 'VecAdd'
  1211. (* Vector addition *)
  1212. DEF VecAdd AS trans | EACH + END;
  1213.  
  1214. SHAR_EOF
  1215. if test -f 'VecSub'
  1216. then
  1217.     echo shar: over-writing existing file "'VecSub'"
  1218. fi
  1219. cat << \SHAR_EOF > 'VecSub'
  1220. (* Vector subtraction *)
  1221. DEF VecSub AS trans | EACH - END;
  1222.  
  1223. SHAR_EOF
  1224. cd ..
  1225. cd ..
  1226. cd ..
  1227. #    End of shell archive
  1228. exit 0
  1229.  
  1230. -- 
  1231.  
  1232. Rich $alz            "Anger is an energy"
  1233. Cronus Project, BBN Labs    rsalz@pineapple.bbn.com
  1234. Moderator, comp.sources.unix    sources@uunet.uu.net
  1235.